define(['./module'],function(logisticsbusinessareamodule){
	'use strict';

	/*=====Begin Of Save and Edit LogisticsBusinessT=====*/
	logisticsbusinessareamodule.controller('logisticsbusinessarea',ServerSaveLogisticsAreaBusiness);
	function ServerSaveLogisticsAreaBusiness($scope,$http,$location){
		$scope.gtparams=[];
		$scope.index=1;
		$scope.addgtparams=function(){
			var param={id:++$scope.index,paramname:'',sort:''};
			$scope.gtparams.push(param);
		}
		
		$scope.remove=function(index){
			$scope.gtparams.splice(index,1);
		}
		
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){
			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加物流商区域价格规则信息';
			$scope.status="1";
			$scope.costway="1"
			//保存数据方法
			$scope.save=function(){
				if(validate()){
					var logisticsbusiness=$scope.logisticsbusiness;
					var overcost=$scope.overcost;
					if(overcost==undefined){
						overcost = "";
					}
					var overvalue=$scope.overvalue;
					if(overvalue==undefined){
						overvalue = "";
					}
					var sendarea=angular.toJson($scope.gtparams);
					var normalvalue=$scope.normalvalue;
					var costway=$scope.costway;
					var status=$scope.status;
					$http({
						method:'POST',
						url:'../mall/logistics/businessarea/save.action',
						data:{
							'logisticsid':logisticsbusiness,
							'overcost':overcost,
							'overvalue':overvalue,
							'sendarea':sendarea,
							'costway':costway,
							'normalvalue':normalvalue,
							'status':status
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
									show:true,
									msg:'物流商区域价格规则信息添加成功'
							}
							$location.path('/logisticsbusinessareament');
//							$scope.errors={
//									show:false
//							}
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
								show:true,
								msg:'系统异常'
						}
					});
				}
			}
		}
		
		//如果operate是edit则执行保存行为
		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/logistics/businessarea/find.action',
					data:{
						'logbusareaid':id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.title='正在对物流商区域价格规则信息进行编辑';
						$scope.lastlogisticsname=data.bean.lastlogisticsname;
						$scope.normalvalue=data.bean.normalvalue;
						$scope.overcost=data.bean.overcost;
						$scope.overvalue=data.bean.overvalue;
						$scope.costway=data.bean.costway;
						$scope.gtparams=angular.fromJson(data.bean.sendarea);
						$scope.status=data.bean.status;
						$scope.lastlogisticsname=data.bean.logisticsname;
						$scope.logisticsnameshow=true;
						$scope.updatebtn={show:true};
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
				//更新数据方法
				$scope.update=function(){
					if(validate()){
						var logisticsbusiness=$scope.logisticsbusiness;
						var overcost=$scope.overcost;
						if(overcost==undefined){
							overcost = "";
						}
						var overvalue=$scope.overvalue;
						if(overvalue==undefined){
							overvalue = "";
						}
						var sendarea=angular.toJson($scope.gtparams);
						var costway=$scope.costway;
						var normalvalue=$scope.normalvalue;
						var status=$scope.status;
						$http({
							method:'POST',
							url:'../mall/logistics/businessarea/update.action',
							data:{
								'logisticsid':logisticsbusiness,
								'overcost':overcost,
								'overvalue':overvalue,
								'sendarea':sendarea,
								'costway':costway,
								'status':status,
								'normalvalue':normalvalue,
								'logbusareaid':id
							}
						}).
						success(function(data,status,headers,config){
							if(data.sucflag){
								$scope.info={
										show:true,
										msg:'物流商区域价格规则信息更新成功'
								}
								$location.path('/logisticsbusinessareament');
							}
						}).
						error(function(data,status,headers,config){
							$scope.errors={
									show:true,
									msg:'系统异常'
							}
						});
					}
				}
			}
		}
		
		//表单字段验证
		function validate(){
			var logisticsbusiness=$scope.logisticsbusiness;
			if(logisticsbusiness==undefined||logisticsbusiness==""){
				$scope.errors={
						show:true,
						msg:"请选择物流商"
				}
				return false;
			}
			return true;
		}
		
		// 查询所有物流商
		$scope.findalllogisticsbusiness=function(){
			$http({
				method:'POST',
				url:'../mall/logistics/business/findAll.action',
				data:{}
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.logisticsbusinesss = data.beanlists;
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
						show:true,
						msg:'系统异常'
				}
			});
		}
	}
	/*=====End Of Save and Edit LogisticsBusinessT=====*/
	
	/*=====Begin Of Find List LogisticsBusinessT=====*/
	logisticsbusinessareamodule.controller('logisticsbusinessarealist',ServerLogisticsBusinessAreaListCtrl);
	//查询列表数据
	function ServerLogisticsBusinessAreaListCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/logistics/businessarea/findByPage.action',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')
		
		$scope.$on('handleRequest',function(){
			
		});
		
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			          DTColumnBuilder.newColumn('logisticsname').withTitle('名称').notSortable(),
			          DTColumnBuilder.newColumn('costway').withTitle('计费方式').notSortable(),
			          DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('versiont').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="edit('+data.id+')"><i class="fa fa-edit"></i></button>';
		}
		
		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}
		
		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/logisticsbusinessarea').search({'operate':'edit','id':id});
		}
		
		$scope.save=function(){
			$location.path('/logisticsbusinessarea').search({'operate':'save'});
		}
		
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/logistics/businessarea/del.action',
					data:{
						'ids':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'批量删除成功'
						}
						reloadData();
					}
					
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}
		
	}
});